<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validateTrigger="bind">
			<uni-forms-item name="price" label="提现金额">
				<view class="sls">{{formData.price / 100}}元</view>
			</uni-forms-item>
			<uni-forms-item name="state" label="提现状态">
				<uni-data-checkbox style="margin-top: 6px;" v-model="formData.state" :localdata="sex"
					@change="notesdefault(formData.state)"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="source" label="提现来源">
				<view class="sls">{{formData.source}}佣金提现</view>
			</uni-forms-item>
			<uni-forms-item label="提现用户">
				<view class="sls">昵称：{{formData.username[0]}}</view>
				<view class="sls">姓名：{{formData.withdra[0].name}}</view>
				<view class="sls">银行卡号：{{formData.withdra[0].bank_card}}</view>
				<view class="sls">开户行：{{formData.withdra[0].bank}}</view>
			</uni-forms-item>
			<uni-forms-item name="notes" label="审核备注">
				<uni-easyinput placeholder="审核回执,通过|失败原因" type="textarea" v-model="formData.notes"></uni-easyinput>
			</uni-forms-item>
			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>

		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/jlg-withdrawal.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'jlg-withdrawal';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"price": null,
				"state": "",
				"source": "",
				"notes": ""
			}
			return {
				formData,
				sex: [{
					text: "审核中",
					value: "审核中"
				}, {
					text: "已打款",
					value: "已打款"
				}, {
					text: "审核失败",
					value: "审核失败"
				}],
				formOptions: {},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onLoad(e) {
			if (e.id) {
				const id = e.id
				this.formDataId = id
				this.getDetail(id)
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((resl) => {
					let price = resl.price
					if (resl.state == "审核失败" && resl.source == "分销") {
						db.collection("jlg-distribution").where({
							"user_id": this.user_id
						}).get().then((res) => {
							let wei = res.result.data[0].with_comm
							let uodr = res.result.data[0].uodr_comm
							let records = res.result.data[0].records
							records.push({
								content: "佣金提现退回",
								price: price,
								time: Date.now()
							})
							db.collection("jlg-distribution").where({
								"user_id": this.user_id
							}).update({
								"with_comm": wei - price,
								"uodr_comm": uodr + price,
								"records": records
							}).then(() => {
								return this.submitForm(resl)
							})
						})
					} else {
						return this.submitForm(resl)
					}
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
					uni.showToast({
						title: '审核成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			},
			notesdefault(e) {
				if (e == "已打款") {
					this.formData.notes = "信息无误，已打款至卡号：" + this.formData.withdra[0].bank_card
				} else if (e == "审核失败") {
					this.formData.notes = "信息有误，佣金已退回，请核对后重新申请"
				} else {
					this.formData.notes = ""
				}
			},

			/**
			 * 获取表单数据
			 * @param {Object} id
			 */
			getDetail(id) {
				uni.showLoading({
					mask: true
				})
				db.collection("jlg-withdrawal,uni-id-users").doc(id).field(
						"price,state,source,user_id.nickname as username,notes,user_id.withdrawal as withdra,user_id._id as usid,create_time"
					)
					.get()
					.then((
						res) => {
						const data = res.result.data[0]
						if (data) {
							this.formData = data
							this.user_id = data.usid[0]
						}
					}).catch((err) => {
						uni.showModal({
							content: err.message || '请求服务失败',
							showCancel: false
						})
					}).finally(() => {
						uni.hideLoading()
					})
			}
		}
	}
</script>
<style>
	.sls {
		margin-top: 10px;
	}
</style>